/**
 * 
 */
package info.niwota.fwrite;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author qiangli
 * 
 */

final public class MenuDialogs {

	public static AlertDialog createMenuDialog(final Context ctx,
			final int title, final MenuCallback cb) {
		return create(ctx, title, menu_items, cb);
	}

	public static AlertDialog createFaceDialog(final Context ctx,
			final int title, final MenuCallback cb) {
		return create(ctx, title, face_items, cb);
	}

	public static Dialog createMoreDialog(final Context ctx, final int title,
			MenuCallback cb) {
		return create(ctx, title, more_items, cb);
	}

	public static Dialog createFlagDialog(final Context ctx, final int title,
			MenuCallback cb) {
		return create(ctx, title, flag_items, cb);
	}

	public static Dialog createSortMenuDialog(final Context ctx, final int title,
			MenuCallback cb) {
		return create(ctx, title, sort_menu_items, cb);
	}
	
	public static Dialog createBreadCrumbDialog(final Context ctx, final MenuCallback cb) {
		final AlertDialog.Builder b = new AlertDialog.Builder(ctx);
		b.setTitle(null);
		b.setInverseBackgroundForced(false);
		//
		List<Document> notes = new ArrayList<Document>();
		final NoteListAdapter adapter = new NoteListAdapter(ctx, notes);
		b.setAdapter(adapter, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				Document d = adapter.getItem(which);
				if (d != null) {
					MenuInfo mi = new MenuInfo();
					mi.data = d;
					cb.perform(mi);
					dialog.dismiss();
				}
			}
		});
		b.setCancelable(true);
		return b.create();
	}

	private static AlertDialog create(final Context ctx, final int title,
			final List<MenuInfo> list, final MenuCallback cb) {
		final AlertDialog.Builder b = new AlertDialog.Builder(ctx);
		b.setTitle(title);
		b.setIcon(R.drawable.ic_dialog_menu_generic);
		b.setInverseBackgroundForced(true);
		//
		final MyListAdapter adapter = new MyListAdapter(ctx, list);
		b.setAdapter(adapter, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				MenuInfo mi = adapter.getMenuInfo(which);
				if (mi != null) {
					cb.perform(mi);
					dialog.dismiss();
				}
			}
		});
		b.setCancelable(true);
		return b.create();
	}

	private static final class MyListAdapter extends BaseAdapter {
		private LayoutInflater mInflater;

		private List<MenuInfo> list;

		private Context context;

		public MyListAdapter(Context context, List<MenuInfo> list) {
			this.context = context;
			this.list = list;
			init();
		}

		private void init() {
			mInflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		public MenuInfo getMenuInfo(int position) {
			if (list == null) {
				return null;
			}
			MenuInfo info = list.get(position);
			return info;
		}

		public int getCount() {
			return (list != null ? list.size() : 0);
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			View view;
			if (convertView == null) {
				view = mInflater.inflate(R.layout.list_item, parent, false);
			} else {
				view = convertView;
			}
			bindView(view, list.get(position));
			return view;
		}

		private final void bindView(View view, MenuInfo info) {
			TextView text = (TextView) view.findViewById(R.id.line1);

			ImageView icon = (ImageView) view.findViewById(R.id.icon);
			text.setText(info.getId()); // id is ref to action title
			text.setTextColor(R.color.black);
			icon.setImageResource(info.getIcon());
		}

	}

	private static final int[] menu_ids = { 
			R.string.action_top,
			R.string.action_compose, 
			R.string.action_sessions, 
			R.string.action_peers,
			R.string.action_archive};
	private static final int[] menu_icons = { 
			R.drawable.top32, 
			R.drawable.send32,
			R.drawable.frite32,
			R.drawable.peers32, 
			R.drawable.folder32};

	private static final int[] more_ids = { R.string.action_flag };
	private static final int[] more_icons = { R.drawable.status_white };

	private static final int[] flag_ids = { R.string.flag_urgent, R.string.flag_red,
			R.string.flag_orange, R.string.flag_yellow, R.string.flag_green,
			R.string.flag_cyan, R.string.flag_blue, R.string.flag_violet,
			R.string.flag_normal, R.string.flag_hold, R.string.flag_done };
	private static final int[] flag_icons = { R.drawable.status_urgent,
			R.drawable.status_red, R.drawable.status_orange,
			R.drawable.status_yellow, R.drawable.status_green,
			R.drawable.status_cyan, R.drawable.status_blue,
			R.drawable.status_violet, R.drawable.status_none,
			R.drawable.status_hold, R.drawable.status_done };
	
	//flag weight values
	private static final double[] flag_values = {20, 17,16,15,14,13,12,11, 0, -10, -20};
	
	public static final int mapFlagIcon(double flag) {
		for (int i = 0; i < flag_values.length; i++) {
			if (flag >= flag_values[i]) {
				return flag_icons[i];
			}
		}
		return R.drawable.empty;
	}
	
	public static double getFlagValues(int id) {
		for (int i = 0; i < flag_ids.length; i++) {
			if (id == flag_ids[i]) {
				return flag_values[i];
			}
		}
		throw new IndexOutOfBoundsException("id: " + id);
	}

	private static final int[] sort_menu_ids = {
		R.string.sort_by_flag, 
		R.string.sort_by_date};
	private static final int[] sort_menu_icons ={0, 0};
	
	private static final List<MenuInfo> menu_items = new ArrayList<MenuInfo>();

	private static final List<MenuInfo> face_items = new ArrayList<MenuInfo>();

	private static final List<MenuInfo> more_items = new ArrayList<MenuInfo>();

	private static final List<MenuInfo> flag_items = new ArrayList<MenuInfo>();

	private static final List<MenuInfo> sort_menu_items = new ArrayList<MenuInfo>();
	
	static {
		
		for (int i = 0; i < menu_ids.length; i++) {
			menu_items.add(new MenuInfo(menu_ids[i], menu_icons[i]));
		}

		for (int i = 0; i < Smiley.face_ids.length; i++) {
			face_items.add(new MenuInfo(Smiley.face_ids[i],
					Smiley.face_icons[i]));
		}

		for (int i = 0; i < more_ids.length; i++) {
			more_items.add(new MenuInfo(more_ids[i], more_icons[i]));
		}

		for (int i = 0; i < flag_ids.length; i++) {
			flag_items.add(new MenuInfo(flag_ids[i], flag_icons[i]));
		}
		
		for (int i = 0; i < sort_menu_ids.length; i++) {
			sort_menu_items.add(new MenuInfo(sort_menu_ids[i], sort_menu_icons[i]));
		}
	}
}
